﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Globalization;

public partial class AllCompanies : System.Web.UI.Page
{
    public string cid;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GridView1.DataSource = CusSqlDataSource;
            GridView1.DataBind();
           
        }
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);


            }
        }

    }
    protected void comps_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (allcompanies.SelectedValue != "-1")
        {
            CusSqlDataSource.SelectCommand = "SELECT * FROM [companies] where  active =" + allcompanies.SelectedValue;

        }
        GridView1.DataSource = CusSqlDataSource;
        GridView1.DataBind();
    }
    protected void act_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (act.SelectedValue != "0")
        {
            CusSqlDataSource.SelectCommand = "SELECT * FROM [companies] where  organization_id =" + act.SelectedValue;

        }
        GridView1.DataSource = CusSqlDataSource;
        GridView1.DataBind();
    }
    protected void display_Click(object sender, EventArgs e)
    {
        LinkButton lk = (LinkButton)sender;
        string[] args = lk.CommandArgument.Split(',');

        CustomerInfo1.ClientName = args[0];
        CustomerInfo1.Country = args[1];
        if (args[3] != "")
        {
            CustomerInfo1.Longs = decimal.Parse(args[3]);
            CustomerInfo1.Lat = decimal.Parse(args[4]);
        }
        CustomerInfo1.Email = args[5];

        GridViewRow gr = (GridViewRow)lk.NamingContainer;
        CustomerInfo1.CompanyName = ((Label)gr.FindControl("co")).Text;
        CustomerInfo1.Address = ((Label)gr.FindControl("add")).Text;
        CustomerInfo1.City = ((Label)gr.FindControl("city")).Text;
        string[] phos = ((Label)gr.FindControl("phone")).Text.Split(',');
        CustomerInfo1.Tele1 = phos[0];
        CustomerInfo1.Tele2 = phos[1];
        displays.Visible = true;
        //foreach (GridViewRow row in GridView1.Rows)
        //{
        //    row.BackColor = System.Drawing.Color.Empty;
        //}
        //GridView1.Rows[gr.RowIndex].BackColor = Color.YellowGreen;



    }
    protected void saves_Click(object sender, EventArgs e)
    {
        //DataSet ds = FilterCustomer1.getTheData();
        //if (ds.Tables.Count > 0)
        //{
        //    GridView1.DataSource = ds.Tables[0];
        //    GridView1.DataBind();
        //}

    }
    protected void delete_Click(object sender, EventArgs e)
    {  
        LinkButton lkb=(LinkButton)sender;
        string myid = lkb.ToolTip;
        // DB.DELETE("customer_season", "int,customer_id," + myid);
        DB.DELETE("subscriptions", "int,company_id," + myid);
        DB.DELETE("companies", "int,id," + myid);
        DB.DELETE("users", "int,id," + lkb.CommandArgument);
        //foreach (GridViewRow row in GridView1.Rows)
        //{
        //    row.BackColor = System.Drawing.Color.Empty;
        //}
        GridView1.DataSource = CusSqlDataSource;
        GridView1.DataBind();
     

    }
    protected void stop_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.BackColor = System.Drawing.Color.Empty;
        }
         LinkButton lk=  (LinkButton)sender;
        string myid =lk.ToolTip;
       Boolean act=Boolean.Parse(lk.CommandArgument);
       string acts = act ? "0" : "1";
        
 
        DB.UPDATE("companies", "int,id," + myid,"active","bit",acts);
        GridView1.DataSource = CusSqlDataSource;
        GridView1.DataBind();

    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        search();
    }
    public void search()
    {

        if (searchfield.Text.Length != 0)
        {


            Label1.Text = "";

            DataSet ds;
            
            string loadsearch = DB.remove_spaces(searchfield.Text);

            if (DropDownList2.SelectedValue == "0")
            {
                ds = DB.quick_search("companies", loadsearch);
            }
            else
            {
                ds = DB.quick_search("companies", loadsearch, DropDownList2.SelectedValue);
            }
            
           
            if (ds.Tables[0].Rows.Count > 0)
            {


                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
                Label1.Visible = false; 

            }
            else
            {

                Label1.Visible = true; 

            }


        }
        else
        {

            Label1.Text = " من فضلك لا يجوز ترك هذا الحقل خاليا";
            Label1.Visible = true;
        }


    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Attributes.Add("onmouseover", "this.Mystyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFA8'");

        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.Mystyle");
        //}
    }





    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lk = (LinkButton)e.Row.FindControl("actstop");
            if (Boolean.Parse(lk.CommandArgument))
            {
                lk.Text = this.GetLocalResourceObject("stopit").ToString();
                
            }
            else
            {
                lk.Text = this.GetLocalResourceObject("activeit").ToString();//"~/companypart/App_LocalResources/CompanySubscription.aspx.resx", 
               

            }

        }

    }
    protected void ImageButton1_Click(object sender, EventArgs e)
    {
        search();
    }
}
